using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using HREDU.Hoang_Tuan;

namespace HREDU.Hoang_Tuan.Classes
{
    public class CTHAMSO
    {
        private Int16 _id_thamso;
        private string _ten_thamso;
        private int _giatriso;
        private DateTime _giatridatetime;
        private string _giatritext;

        public Int16 ID_THAMSO
        {
            get { return _id_thamso; }
            set { _id_thamso = value; }
        }

        public string TEN_THAMSO
        {
            get { return _ten_thamso; }
            set { _ten_thamso = value; }
        }

        public int GIATRISO
        {
            get { return _giatriso; }
            set { _giatriso = value; }
        }

        public DateTime GIATRIDATETIME
        {
            get { return _giatridatetime; }
            set { _giatridatetime = value; }
        }

        public string GIATRITEXT
        {
            get { return _giatritext; }
            set { _giatritext = value; }
        }
    }

    public class CTHAMSOCollection
    {
        //Ten cac gia tri tham so hien dang dung
        public static string parLuongCB = "LUONGCB";
        public static string parSNGB = "SNGB";
        public static string parTNVK_1 = "TNVK_1";
        public static string parTNVK_2 = "TNVK_2";

        public static int GetGiaTriSo(string pTen)
        {
            int KQ = 0;
            string strSQL;
            strSQL = "SELECT * FROM THAMSO where ten_thamso = '" + pTen + "'";
            SqlDataReader objReader = null;
            try
            {
                objReader = MITI.BaseDB.Instance.ExecSql_DataReader(strSQL);
                while (objReader.Read())
                {
                     KQ = objReader.GetInt32(2);
                }
                if ((objReader != null))
                {
                    objReader.Close();
                }
                MITI.BaseDB.Instance.CloseConnect();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return KQ;
        }

        public static DateTime GetGiaTriDateTime(string pTen)
        {
            DateTime KQ = MyDate.DateNull();
            string strSQL;
            strSQL = "SELECT * FROM THAMSO where ten_thamso = '" + pTen + "'";
            SqlDataReader objReader = null;
            try
            {
                objReader = MITI.BaseDB.Instance.ExecSql_DataReader(strSQL);
                while (objReader.Read())
                {
                    KQ = objReader.GetDateTime(2);
                }
                if ((objReader != null))
                {
                    objReader.Close();
                }
                MITI.BaseDB.Instance.CloseConnect();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return KQ;
        }

        public static string GetGiaTriText(string pTen)
        {
            string KQ = "";
            string strSQL;
            strSQL = "SELECT * FROM THAMSO where ten_thamso = '" + pTen + "'";
            SqlDataReader objReader = null;
            try
            {
                objReader = MITI.BaseDB.Instance.ExecSql_DataReader(strSQL);
                while (objReader.Read())
                {
                    KQ = objReader.GetString(2);
                }
                if ((objReader != null))
                {
                    objReader.Close();
                }
                MITI.BaseDB.Instance.CloseConnect();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return KQ;
        }

        public static int UpdateGiaTriSo(string pTen, int pGiaTri)
        {
            int lRowsAffected;
            string sUpdate = "UPDATE THAMSO SET";
            sUpdate += " giatriso = " + pGiaTri;
            sUpdate += " WHERE ten_thamso = '" + pTen + "'";
            lRowsAffected = (int)MITI.BaseDB.Instance.ExecSql_NoneQuery(sUpdate);
            return lRowsAffected;
        }

        public static int UpdateGiaTriDateTime(string pTen, DateTime pGiaTri)
        {
            int lRowsAffected;
            string sUpdate = "UPDATE THAMSO SET";
            sUpdate += " giatridatetime = " + pGiaTri;
            sUpdate += " WHERE ten_thamso = '" + pTen + "'";
            lRowsAffected = (int)MITI.BaseDB.Instance.ExecSql_NoneQuery(sUpdate);
            return lRowsAffected;
        }

        public static int UpdateGiaTriText(string pTen, string pGiaTri)
        {
            int lRowsAffected;
            string sUpdate = "UPDATE THAMSO SET";
            sUpdate += " giatritext = " + pGiaTri;
            sUpdate += " WHERE ten_thamso = '" + pTen + "'";
            lRowsAffected = (int)MITI.BaseDB.Instance.ExecSql_NoneQuery(sUpdate);
            return lRowsAffected;
        }

        public static CTHAMSO GetObjectByDataReader(SqlDataReader objReader)
        {
            CTHAMSO objThamSo = new CTHAMSO();
            objThamSo.ID_THAMSO = objReader.GetInt16(0);
            objThamSo.TEN_THAMSO = objReader.GetString(1);
            objThamSo.GIATRISO = (objReader.GetValue(2) == DBNull.Value ? -1 : objReader.GetInt32(2));
            objThamSo.GIATRIDATETIME = (objReader.GetValue(3) == DBNull.Value ? MyDate.DateNull() : objReader.GetDateTime(3));
            objThamSo.GIATRITEXT = (objReader.GetValue(4) == DBNull.Value ? null : objReader.GetString(4));
            return objThamSo;
        }
    }
}
